27 #define _DEFAULT_SOURCE // for cfmakeraw 30 #if (defined __APPLE__) || (defined LINUX) || (defined __linux__) 34 #include <sys/ioctl.h> 54 uint32_t timeout_period_ms;
73 const char dev_dir[] =
"/dev";
74 const char search_pattern[] =
"cu.usb";
76 uint32_t num_available_ports = 0;
86 dir_handle = opendir(dev_dir);
88 if (dir_handle != NULL)
91 struct dirent* dir_entry;
93 while ((dir_entry = readdir(dir_handle)) != NULL)
98 char current_device[128];
100 if (strstr(dir_entry->d_name, search_pattern) == NULL)
108 strcpy(current_device, dev_dir);
109 strcat(current_device,
"/");
110 strcat(current_device, dir_entry->d_name);
112 name_length = strlen(current_device);
114 if (num_available_ports > 0)
120 if (name_length < buffer_size)
122 if (num_available_ports > 0)
124 strcat(port_list,
";");
127 strcat(port_list, current_device);
130 buffer_size -= name_length;
134 ++num_available_ports;
138 closedir(dir_handle);
141 return num_available_ports;
145 static bool file_exists(
const char* path)
147 FILE* fh = fopen(path,
"r");
156 static bool check_uevent(
const char* path,
const char* vid,
const char* pid)
160 long vid_ = strtol(vid, NULL, 16), pid_ = strtol(pid, NULL, 16);
162 FILE* fh = fopen(path,
"r");
166 while (fgets(buffer,
sizeof(buffer), fh))
169 if (strncmp(buffer,
"PRODUCT=", 8) != 0)
173 char* s_vid = buffer + 8;
174 if ((p = strchr(s_vid,
'/')) == NULL)
179 if ((p = strchr(s_pid,
'/')) == NULL)
183 if ((strtol(s_vid, NULL, 16) == vid_) && (strtol(s_pid, NULL, 16) == pid_))
197 uint32_t num_available_ports = 0;
199 memset(port_list, 0, buffer_size);
201 const char* sysdir =
"/sys/class/tty/";
202 DIR* dir_handle = opendir(sysdir);
203 if (dir_handle == NULL)
206 struct dirent* dir_entry;
207 while ((dir_entry = readdir(dir_handle)) != NULL)
210 const char* fname = dir_entry->d_name;
211 if (strncmp(fname,
"ttyACM", 6) != 0)
215 char path[PATH_MAX] = { 0 };
216 snprintf(path,
sizeof(path) - 1,
"%s/%s/device/uevent", sysdir, fname);
219 char port[512] = { 0 };
220 snprintf(port,
sizeof(port) - 1,
"/dev/%s", fname);
221 if (!file_exists(port))
224 size_t len = strlen(port);
225 if (num_available_ports > 0)
228 if (buffer_size > len)
231 if (num_available_ports)
232 strcat(port_list,
";");
234 strcat(port_list, port);
239 num_available_ports++;
243 closedir(dir_handle);
244 return num_available_ports;
250 struct termios options;
260 com_port->handle = open(port_name, O_RDWR | O_NOCTTY);
262 if (com_port->handle == -1)
271 if (ioctl(com_port->handle, TIOCEXCL) == -1)
281 if (tcgetattr(com_port->handle, &options) == -1)
298 options.c_cc[VMIN] = 0;
299 options.c_cc[VTIME] = 0;
304 options.c_cflag |= CLOCAL;
307 if (tcsetattr(com_port->handle, TCSANOW, &options) == -1)
310 com_port->timeout_period_ms = 1000;
321 close(com_port->handle);
330 return write(com_port->handle, data, num_bytes);
335 void* data,
size_t num_requested_bytes)
337 size_t num_received_bytes = 0;
338 char* read_buffer = (
char*)data;
339 struct timespec time_of_last_byte;
342 clock_gettime(CLOCK_MONOTONIC, &time_of_last_byte);
343 while (num_received_bytes < num_requested_bytes)
345 ssize_t num_bytes = read(com_port->handle,
346 read_buffer + num_received_bytes,
347 num_requested_bytes - num_received_bytes);
348 num_received_bytes += num_bytes;
351 struct timespec current_time;
352 clock_gettime(CLOCK_MONOTONIC, ¤t_time);
355 time_of_last_byte = current_time;
359 if ((current_time.tv_sec * 1000 + current_time.tv_nsec / 1000000) >
360 (time_of_last_byte.tv_sec * 1000 +
361 time_of_last_byte.tv_nsec / 1000000 +
362 com_port->timeout_period_ms))
368 return num_received_bytes;
373 com_port->timeout_period_ms = timeout_period_ms;
void com_set_timeout(com_t *com_port, uint32_t timeout_period_ms)
This function changes the timeout period of an open COM port.
size_t com_get_data(com_t *com_port, void *data, size_t num_requested_bytes)
This function reads data from an open COM port.
void com_close(com_t *com_port)
This function closes the specified COM port.
This file defines the API to access a serial communication port.
size_t com_send_data(com_t *com_port, const void *data, size_t num_bytes)
This function sends data through an open COM port.
com_t * com_open(const char *port_name)
This functions opens the specified COM port.
uint32_t com_get_port_list(char *port_list, size_t buffer_size)
This function returns a list of available COM ports.